<template>
    <view class="uni-container">
        <uni-forms ref="formRef" :model="form" label-width="80px" :border="true" :rules="rules" err-show-type="toast">
            <uni-forms-item label="新密码" name="password">
                <uni-easyinput v-model="form.password" type="password" placeholder="请输入新密码" :inputBorder="false"/>
            </uni-forms-item>

            <uni-forms-item label="再次输入" name="password2" style="border-bottom: 1px #eee solid;">
                <uni-easyinput v-model="form.password2" type="password" placeholder="再次输入新密码" :inputBorder="false"/>
            </uni-forms-item>
        </uni-forms>
        <view type="primary" @click="updatePassword()" class="uni-btn" style="margin-top:50px;">确认</view>
    </view>
</template>

<script>
import {UserApi} from '@/api/user/UserApi.js'

export default {
    data() {
        return {
			Config: this.$Config,
			Common: this.$Common,
			Auth: this.$Auth,
			ShareUtil: this.$ShareUtil,
			State: this.$store.state,
			Dict: this.$Dict,

            form: {
                password: "",
                password2: ""
            },
            rules: {
                password: {
                    rules: [{
                        required: true,
                        errorMessage: '请输入新密码',
                    },
                        {
                            minLength: 6,
                            maxLength: 20,
                            errorMessage: '新密码长度在 {minLength} 到 {maxLength} 个字符',
                        }
                    ]
                },
                password2: {
                    rules: [{
                        required: true,
                        errorMessage: '请再次输入新密码',
                    }, {
                        minLength: 6,
                        maxLength: 20,
                        errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
                    }, {
                        validateFunction: (rule, value, data, callback) => {
                            // 异步需要返回 Promise 对象
                            return new Promise((resolve, reject) => {
                                if (value === this.form.password) {
                                    // 通过返回 resolve
                                    resolve()
                                } else {
                                    // 不通过返回 reject(new Error('错误信息'))
                                    reject(new Error('两次新密码输入不一致'))
                                }
                            })
                        }
                    }]
                },
            }
        }
    },
    onLoad() {
    },
    onReady() {
        this.$refs.formRef.setRules(this.rules)
    },
    methods: {
        initPage: function () {
        },
        updatePassword() {
			let that = this
            this.$refs.formRef.validate().then(res => {
                let data = {
                    password: this.form.password
                }
                uni.showModal({
                    title: '提示',
                    content: '确定修改密码吗？',
                    success: function (res) {
                        if (res.confirm) {
                            UserApi.updatePassword(data).then((response) => {
								uni.showToast({title: '修改成功！'});
                                that.Common.navigateBackRefresh()
                            })
                        }
                    }
                })
            }).catch(err => {

            })
        }
    }
}
</script>

<style lang="scss">
page{background: #FFFFFF;}
</style>
